package com.linkage.educloud.ah.view;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;

import com.linkage.educloud.ah.mvp.IBasicView;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;


/**
 * <desc>
 * Created by The Moss on 2018/7/24.
 */

public abstract class AbstractActivity extends RxAppCompatActivity implements IBasicView {
    protected String TAG = this.getClass().getSimpleName();
    protected FragmentManager mManager;
    protected FragmentTransaction mTransaction;
    boolean isTranslate = true;
    ZLoadingDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusLayoutTranslate();
        mManager = getSupportFragmentManager();
        mTransaction = mManager.beginTransaction();
    }

    protected abstract int attachLayoutRes();
    protected void setStatusLayoutTranslate(){
        if (!isTranslate) {
            return;
        }
        //适配状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /** 如果不需要透明状态栏，在子类的oncreate之前调用，设置false
     * @param isTranslate
     */
    protected void setStatusTranslate(boolean isTranslate){
        this.isTranslate = isTranslate;
    }
    @Override
    public LifecycleTransformer bindLifecycle() {
        return this.bindToLifecycle();
    }

    protected void showZloading(){
        if (dialog == null) {
            dialog = new ZLoadingDialog(this);
        }
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.RED)//颜色
                .setHintText("Loading...")
                .setHintTextSize(-1) // 设置字体大小 dp
                .setHintTextColor(Color.GRAY)  // 设置字体颜色
                .setDurationTime(0.5) // 设置动画时间百分比 - 0.5倍
                .setDialogBackgroundColor(Color.parseColor("#CC111111")) // 设置背景色，默认白色
                .show();
    }
    protected void hideZloading(){
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}